/*
 * Copyright (c) 2025 EasyAdmin
 * All rights reserved.
 *
 * This file is part of the [EasAdmin] project.
 * Unauthorized copying of this file, via any medium, is strictly prohibited.
 * Proprietary and confidential.
 *
 * Author: EasyAdmin ( 1073602@qq.com )
 * Website: https://www.xhxiao.com
 */
package ${table.packageName}.controller;

import com.easy.framework.common.model.R;
import com.easy.framework.common.validate.Add;
import com.easy.framework.common.validate.Update;
import com.easy.framework.database.page.Pagination;
import com.easy.framework.poi.utils.ExcelUtils;
import com.easy.framework.web.base.BaseController;
import com.easy.module.log.annotation.Log;
import com.easy.module.log.enums.Operation;
import ${table.packageName}.entity.${table.className};
import ${table.packageName}.query.${table.className}Query;
import ${table.packageName}.service.I${table.className}Service;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * ${table.tableComment} 控制器
 *
 * @author ${table.author}
 * @date ${date}
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("<#if table.moduleName??>${table.moduleName}/</#if>${table.path}")
public class ${table.className}Controller extends BaseController
{

    final I${table.className}Service ${table.classNameLowerFirst}Service;

    /**
    * 获取${table.tableComment}分页列表
    */
    @PreAuthorize("@perm.hasPermission('${table.sign}$list')")
    @GetMapping("/page")
    public R<Pagination<${table.className}>> page(${table.className}Query query) {
        return R.success(${table.classNameLowerFirst}Service.page(query));
    }

    /**
    * 获取${table.tableComment}列表
    */
    @PreAuthorize("@perm.hasPermission('${table.sign}$list')")
    @GetMapping("/list")
    public R<List<${table.className}>> list(${table.className}Query query) {
    return R.success(${table.classNameLowerFirst}Service.list(query));
    }

    @Log(title = "${table.tableComment}管理", type = Operation.EXPORT)
    @PreAuthorize("@perm.hasPermission('${table.sign}$export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ${table.className}Query query) throws IOException {
        List<${table.className}> list = ${table.classNameLowerFirst}Service.list(query);
        ExcelUtils.exportExcel(response, list, "${table.tableComment}数据");
    }

    /**
    * 根据${table.tableComment}编号获取详细信息
    */
    @PreAuthorize("@perm.hasPermission('${table.sign}$query')")
    @GetMapping(value = "/{id}")
    public R<${table.className}> get(@PathVariable("id") Long id) {
        return R.success(${table.classNameLowerFirst}Service.getById(id));
    }

    /**
    * 新增
    */
    @PreAuthorize("@perm.hasPermission('${table.sign}$add')")
    @Log(title = "${table.tableComment}管理", type = Operation.INSERT)
    @PostMapping
    public R add(@Validated({Add.class}) @RequestBody ${table.className} model) {
        return R.result(${table.classNameLowerFirst}Service.save(model));
    }

    /**
    * 修改
    */
    @PreAuthorize("@perm.hasPermission('${table.sign}$edit')")
    @Log(title = "${table.tableComment}管理", type = Operation.UPDATE)
    @PutMapping
    public R edit(@Validated({Update.class}) @RequestBody ${table.className} model) {
        return R.result(${table.classNameLowerFirst}Service.updateById(model));
    }

    /**
    * 删除
    */
    @PreAuthorize("@perm.hasPermission('${table.sign}$remove')")
    @Log(title = "${table.tableComment}管理", type = Operation.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable("ids") Long[] ids) {
        return R.result(${table.classNameLowerFirst}Service.removeBatchByIds(List.of(ids)));
    }

}